/**
 * Created by hubo on 2016/4/18.
 */
var table;
$(function() {
    // 表格
    table = $('#dataList').dataTable({
        "fnServerData" : function(sSource, aoData, fnCallback) {

            var contactPerson = document.getElementById("contactPerson").value;
            var name = document.getElementById("name").value;
            var method = document.getElementById("method").value;
            var data = {"start":aoData.start};
            var matcher={};
            if(contactPerson!=""){
                matcher.contactPerson=contactPerson;
            }
            if(name!=""){
                matcher.name=name;
            }
            if(method!=""){
                matcher['permission.method']=method;
            }

            if(!jQuery.isEmptyObject(matcher)){
                data.matcher=JSON.stringify(matcher);
            }
            data.orderBy=JSON.stringify({createDate:-1});
            //表格渲染控件
            $.ajax({
                "dataType" : 'json',
                "type" : "POST",
                "url" : ctx + "/developer/query",
                "data" : data,
                "success" : function(resp){
                    var obj=resp;
                    obj.iTotalRecords=resp.count;
                    obj.iTotalDisplayRecords=resp.count
                    //obj.recordsDisplay=resp.count
                    fnCallback(obj);
                }
            });
        },
        "aoColumnDefs": [
            {
                sDefaultContent: '',
                aTargets: [ '_all' ]
            }
        ],
        sAjaxDataProp:'result',
        "aoColumns" : [null,{
            "data" : "name"
        }, {
            "data" : "shortName"
        }, {
            "data" : "email"
        }, {
            "data" : "contactPerson"
        }, {
            "data" : "contactNum"
        },  {
            "data" : "state"
        },  {
            "data" : "remarks"
        }, null],
        "fnRowCallback" : function(nRow, aData, iDisplayIndex) {
            $('td:eq(0)', nRow).html(iDisplayIndex+1);
            if(aData.state != 'N') {
                $('td:eq(6)', nRow).html('启用');
            }else{
                $('td:eq(6)', nRow).html('停用');
            }
            $('td:eq(8)', nRow).html(getUrl(aData));
        },
        "iDisplayLength": 20,//每页显示20条数据
        "bLengthChange":false/*每页长度禁止选择*/
    });

    /**
     * 搜索
     */
    $('#search').click(function() {
        table.fnDraw();
    });


});

function getUrl(data) {
    var str = '';
    str += '<a href="#" onclick=update(\''+data.partner+'\') class="pRight"><i class="fa text-navy ">编辑</i></a>&nbsp;&nbsp;';
    str += '<a href="#" onclick="interfaceList(\'' + data.partner +'\')" class="pRight"><i class="fa text-navy">接口授权</i></a>&nbsp;&nbsp;';
    str += '<a href="#" onclick="whiteAdd(\'' + data.partner +'\')" class="pRight"><i class="fa text-navy">白名单</i></a>&nbsp;&nbsp;';
    str += '<a href="#"  onclick=ExtParamList(\''+data.partner+'\') class="pRight"><i class="fa text-navy">查询扩展参数</i></a>&nbsp;&nbsp;';
    if(data.state == "N"){
        str += '<a href="#" onclick="open_(\'' + data.partner+'\')" class="pRight"><i class="fa text-navy" >启用</i></a>&nbsp;&nbsp;';
    }else if(data.state == "Y"){
        str += '<a href="#" onclick="close_(\'' + data.partner +'\')" class="pRight"><i class="fa text-navy" >关闭</i></a>&nbsp;&nbsp;';
    }
    return str;
}


//启用状态
function open_(partner){

    $.ajax({
        url:ctx+"/developer/open",
        type:'post',
        data:{
            "partner":partner
        },
        dataType:'json',
        timeout:ajaxTimeout,
        error:function(json){
            toastr.error("开启失败")
        },
        success:function(json){
            //重新渲染table
            if(json.returnCode=='T'){
                toastr.success(json.returnMsg)
            }else{
                toastr.error(json.returnMsg)
            }
            table.fnPageChange(getPageNum(table,false));
        }
    });
}
//关闭状态
function close_(partner){

    $.ajax({
        url:ctx+"/developer/stop",
        type:'post',
        data:{
            "partner":partner
        },
        dataType:'json',
        timeout:ajaxTimeout,
        error:function(json){
            toastr.error("关闭失败")
        },
        success:function(json){
            //重新渲染table
            if(json.returnCode=='T'){
                toastr.success(json.returnMsg)
            }else{
                toastr.error(json.returnMsg)
            }
            table.fnPageChange(getPageNum(table,false));
        }
    });
}


function getPageNum(dataTable,isDelete){
    var currentPageLength = table.fnGetNodes().length;//获取当前数据的条数
    var tableSetings=dataTable.fnSettings();
    var paging_length=tableSetings._iDisplayLength;//当前每页显示多少
    var page_start=tableSetings._iDisplayStart;//当前页开始
    var page=Div(page_start,paging_length,currentPageLength,isDelete);
    return page;
};


// 编辑
function update(method) {
    location.href=ctx+"/developer/updateUI/"+method;
}
//接口列表
function interfaceList(partner){

    location.href=ctx+"/send/developer/interfaceList?partner="+partner;
}

//扩展列表
function ExtParamList(partner){

    location.href=ctx+"/send/developer/paramList?partner="+partner;
}
//跳转白名单页面
function whiteAdd(partner){

    location.href=ctx+"/send/developer/whiteAdd?partner="+partner;
}
function getDate(date) {
    var year = date.substring(0, 4);
    var month = date.substring(4, 6);
    var day = date.substring(6);
    return year + "-" + month + "-" + day;

}

Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "H+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

function strDateFormat(strDate){

    var date = new Date(strDate).Format("yyyy-MM-dd HH:mm:ss");
    return date;
}
